<?php

class AnkaraPharmacyParser 
{
	
	private $rawdata;
	private $coordinateOffset = 0;  /* this is the offset when finding latitude and longitude in string, it is initialized in finding first element and 
					increased regularly in finding every element to find the next element more quickly*/
				     		
	
	public function parsePharmacies($rawdata)
	{	
		$this->rawdata = $rawdata;
		$dom = new DOMDocument('1.0', 'iso-8859-9');
		$dom->loadHTML($this->rawdata); // burada loadHtmlFile kullanilabilir.
		$domContainerPanel = $dom->getElementById("ctl00_Middle_NobetciEczaneler01_MainContainer_Panel")->getElementsByTagName("div");
		$pharmacyList = array();
		if ($domContainerPanel->length == 1) {
			$pharmacyRawList = $domContainerPanel->item(0)->getElementsByTagName("span");			
			
			for ($i = 0; $i < $pharmacyRawList->length; $i++) 
			{
				if ($pharmacyRawList->item($i)->childNodes->length == 5) 
				{
					$pharmacyName = iconv("UTF-8", "ISO-8859-1", trim($pharmacyRawList->item($i)->childNodes->item(0)->nodeValue)) . " ECZANESİ";
					$pharmacyPhone =  iconv("UTF-8", "ISO-8859-1", trim($pharmacyRawList->item($i)->childNodes->item(2)->nodeValue));
					$pharmacyPhone = str_replace(" ", "", $pharmacyPhone);
					if (strpos($pharmacyPhone, "312") === 0) {
						$pharmacyPhone = "0" . $pharmacyPhone;
					}
					else if(strpos($pharmacyPhone, "318") === 0){
						$pharmacyPhone = "0" . $pharmacyPhone;
					}
					else if (strpos($pharmacyPhone, "0312") !== 0) {
						$pharmacyPhone = "0312" . $pharmacyPhone;
					}
					$pharmacyAddress = iconv("UTF-8", "ISO-8859-1", trim($pharmacyRawList->item($i)->childNodes->item(4)->nodeValue));	
					$pharmacyAddress = str_replace('\'', '`', $pharmacyAddress);
					$coordiates = $this->getCoordinates($i);
					array_push($pharmacyList, new Pharmacy($pharmacyName, $pharmacyPhone, $pharmacyAddress, $coordiates["latitude"], $coordiates["longitude"], 0));
				}
				else {
					echo "Error: HTML format has changed in parsing pharmacy details";	
				}			
			}
		}
		else {
			echo "Error: HTML format has changed in finding pharmacy container";
		}
		return $pharmacyList;
	}
	
	private function getCoordinates($pharmacyNo)
	{
		if ($pharmacyNo == 0) {
		//	$this->coordinateOffset = stripos($this->rawdata, "var ctl00_Middle_GoogleMap1_Map");			
			$this->coordinateOffset = stripos($this->rawdata, "ctl00_Middle_GoogleMap1_Map.addMarker");
			
		}
		else {
			$pharmacyNo--;
			$this->coordinateOffset = stripos($this->rawdata, "ctl00_Middle_GoogleMap1_Map.Markers[".$pharmacyNo."]", $this->coordinateOffset);	
		}
		
		$latitudeKey = "\"Latitude\":";
		$latitudeStartIndex = stripos($this->rawdata, $latitudeKey, $this->coordinateOffset) + strlen($latitudeKey); 
		$latitudeEndIndex = stripos($this->rawdata, ",", $latitudeStartIndex);
		$latitude = substr($this->rawdata, $latitudeStartIndex, $latitudeEndIndex - $latitudeStartIndex);
		
		$longitudeKey = "\"Longitude\":";
		$longitudeStartIndex = stripos($this->rawdata, $longitudeKey, $this->coordinateOffset) + strlen($longitudeKey); 
		$longitudeEndIndex = stripos($this->rawdata, ",", $longitudeStartIndex);
		$longitude = substr($this->rawdata, $longitudeStartIndex, $longitudeEndIndex - $longitudeStartIndex);
		
		$coordinates = array("latitude" => $latitude, "longitude" => $longitude);
		
		return $coordinates;		
	}	
}


class Pharmacy {
	
		private $name;
		private $phone;
		private $address;
		private $latitude;
		private $longitude;	
		private $URL;
		
		public function __construct($name, $phone, $address, $latitude, $longitude, $URL)
		{
			$this->name = $name;
			$this->phone = $phone;
			$this->address = $address;
			$this->latitude = $latitude;
			$this->longitude = $longitude;
			$this->URL = $URL;
		}
	
		public function getName()
		{
			return $this->name;
		}
		
		public function getPhone()
		{
			return $this->phone;
		}
		
		public function getAddress()
		{
			return $this->address;
		}	
		
		public function getLatitude()
		{
			return $this->latitude;
		}
		
		public function getLongitude()
		{
			return $this->longitude;
		}
		
		public function getURL()
		{
			return $this->URL;
		}
}
?>